mod mutex_test;

use bytes::{Buf, BufMut, BytesMut};
use log::info;
use rpc::codec::{RpcEncode};
use rpc::router::{DoRouter, RouterFactory};
use rpc::{RpcContext, RpcResult};
use crate::mutex_test::{atomic_isize_test, mpsc_test, mutex_test};

fn main()->RpcResult<()> {
    env_logger::init();
    test_router(1, 2);

    let test_router_factory = TestRouterRouterFactory;
    let mut bytes_mut = BytesMut::new();
    bytes_mut.put_i32(1);
    bytes_mut.put_i32(22);

    let mut rpc_context = RpcContext::test_context_with_codec_type("json");
    let mut t = test_router_factory.parse(&rpc_context, bytes_mut)?;
    let tt = t.execute(&mut rpc_context);
    if let Ok(tt) = tt {
        // let mut byte_mut = BytesMut::new();
        // let result = tt.encode(&mut byte_mut);
        // if let Ok(result) = result {
        //     info!("result:: {:?}", encode(&mut byte_mut));
        // }
    }


    // tests();
    //
    // let testRouterRouterFactory = TestRouterRouterFactory;
    // let router = RpcRouter::new()
    //     .router("/demo", router!(TestRouter));

    mutex_test();
    atomic_isize_test();
    mpsc_test();

    info!("end!!!!!!!!!!!!!!!!!!!!");
    Ok(())
}


fn encode(buf: &mut BytesMut) -> i32{
    buf.get_i32()
}

fn test(hello: &str) {
    info!("Hello, {}!", hello);
}

// #[intercept_method]
fn test_1() -> i32 {
    21
}

fn add(a: i32, b: i32) -> i32 {
    a + b
}

// #[receive_router]
// fn test_router(a: i32, b: i32) -> i32 {
//     a + b
// }

fn test_router(a: i32, b: i32) -> i32 {
    a + b
}

pub struct TestRouterRouterFactory;
#[derive(Debug)]
pub struct TestRouterRouter {
    pub a: i32,
    pub b: i32,
}

impl RouterFactory for TestRouterRouterFactory {
    fn parse(&self, _context: &RpcContext, byte_muts: BytesMut) -> RpcResult<Box<dyn DoRouter>> {
        let mut bytes_mut = byte_muts;
        Ok(Box::new(TestRouterRouter { a: 11, b: 12 }))
    }
}
impl DoRouter for TestRouterRouter {
    fn execute(&mut self, _rpc_context: &mut RpcContext) -> RpcResult<Box<dyn RpcEncode>> {
        let result = test_router(self.a, self.b);
        Ok(Box::new(result))
    }
}

